package com.lky.web.useradmin;

import com.lky.entity.User;
import com.lky.service.UserService;
import com.lky.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/useradmin")
public class UserCheckController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/checkuser",method = RequestMethod.GET)
    @ResponseBody
    /**
     * 通过userName获取用户，判断用户是否唯一
     */
    public Map<String,Object> getUserListByName(HttpServletRequest request){
        String username= HttpServletRequestUtil.getString(request,"username");
        Map<String,Object> modelMap = new HashMap<String,Object>();
        List<User> userList = new ArrayList<User>();
        int queryNum;
        try{
            userList = userService.queryUserByName(username);
            //获得用户名对应的用户数量
            queryNum = userList.size();
            //当queryNum>0时，表示用户已存在
            if(queryNum > 0) {
                modelMap.put("success",false);
            }else {
                modelMap.put("success",true);
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;

    }


}
